package com.ems.controller;

import com.ems.entity.TEmp;
import com.ems.service.ITEmpService;
import com.ems.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZZH
 * @since 2022-06-27
 */
@RestController
@RequestMapping("/t-emp")
public class TEmpController {
    @Autowired
    ITEmpService itEmpService;

    @GetMapping("/getlist")
    public ResultUtil getListEmp(){
        List<TEmp> list = itEmpService.getListEmp();
        if (list != null){
            return ResultUtil.success(list);
        }
        else {
            return ResultUtil.fail("获取失败");
        }
    }

    @PostMapping("/delete")
    public ResultUtil deleteEmp(@RequestParam Integer id){
        int num = itEmpService.deleteEmp(id);
        if (num > 0) {
            return ResultUtil.success(num);
        }
        else {
            return ResultUtil.fail("删除失败");
        }
    }

    @PostMapping("/add")
    public ResultUtil addEmp(@RequestBody TEmp tEmp){
        int num = itEmpService.addEmp(tEmp);
        if (num > 0){
            return ResultUtil.success("添加成功");
        }
        else {
            return ResultUtil.fail("添加失败");
        }
    }

    @PostMapping("/update")
    public ResultUtil updateEmp(TEmp tEmp){
        int num = itEmpService.updateEmp(tEmp);
        if(num > 0){
            return ResultUtil.success("修改成功");
        }
        else {
            return ResultUtil.fail("修改失败");
        }
    }

    /**
     * 人力资源的利用效率
     * @return
     */
    @GetMapping("/geteffec")
    public ResultUtil getEffective(){
        return ResultUtil.success(itEmpService.effective());
    }

    /**
     * 根据姓名模糊查询
     * @param name
     * @return
     */
    @PostMapping("/seleteByname")
    public ResultUtil selectByname( @RequestParam String name){
        List<TEmp> tEmp = itEmpService.selectBynum(name);
        if(tEmp != null){
            return ResultUtil.success(tEmp);
        }
        else {
            return ResultUtil.fail("查找失败");
        }
    }

    /**
     * 根据工号查询员工
     * @param code
     * @return
     */
    @PostMapping("/selectByCode")
    public ResultUtil selectByCode(@RequestParam Integer code){
        TEmp tEmp = itEmpService.selectByCode(code);
        if (tEmp != null){
            return ResultUtil.success(tEmp);
        }
        else{
            return ResultUtil.fail("查找失败");
        }
    }

    /**
     * 获取各种状态的员工人数
     * @return
     */
    @GetMapping("/getAllkindEmp")
    public ResultUtil getAlKindEmp(){
        HashMap<String,Integer> ans = itEmpService.kindNumEmp();
        if (ans.size() > 0){
            return ResultUtil.success(ans);
        }
        else {
            return ResultUtil.fail("获取失败");
        }
    }


}
